/**
 * author: Anibal José Rindisbacher Calligaro
 * email: anibal784@gmail.com
 * fecha: 03/11/2010
 */

var TextEditor = new Class
({
    initialize: function(onFinishEdit)
    {
        this.open = false;
        this.was_cancel = false;
        this._div = new Element('div', {
            styles: {
                visibility: 'hidden',
            }
        });

        this._input = new Element('textarea', {
            cols: '25',
            rows: '1',
            events: {
                keyup: function(e)
                {
                    if(e.key == 'enter'){
                        var _rows = Number.from(this.get('rows'));
                        if(_rows > 10){
                            return;
                        }
                        this.set('rows', _rows + 1);
                    }
                }
            }
        });
        this._input.inject(this._div);
        this._div.inject(document.body);
        (new Element('br')).inject(this._div);

        var _this = this;
        var btn_aceptar = new Element('input', {
            type: 'button',
            text: 'Aceptar',
            events: {
                click: function(e)
                {
                    _this.endEdit(false);
                }
            }
        });

        btn_aceptar.inject(this._div);

        var btn_cancelar = new Element('input', {
            type: 'button',
            text: 'cancelar',
            events: {
                click: function(e)
                {
                    _this.endEdit(true);
                }
            }

        });

        btn_cancelar.inject(this._div);

        this._onFinish = onFinishEdit;
    },

    beginEdit: function(_left, _top, prev_text)
    {
        this._input.set('value', prev_text);
        this._input.set('rows', 1);
        this._div.setStyles({
            visibility: 'visible',
            position: 'absolute',
            left: _left,
            top: _top
        });
        this.open = true;
    },

    endEdit: function(was_cancel)
    {
        this.was_cancel = was_cancel;
        this._div.setStyle('visibility', 'hidden');
        this._onFinish.apply(null, [this]);
        this.open = false;
    },

    getText: function()
    {
        return this._input.value;
    }
});

